package com.yunxin.core.infra.persistence.repository.contact;

import com.yunxin.core.domain.model.contact.ContactMessageSendTaskExecute;
import com.yunxin.core.domain.model.contact.ContactMessageSendTaskExecuteRepository;
import com.yunxin.core.infra.persistence.converter.ContactConverter;
import com.yunxin.core.infra.persistence.entity.contact.ContactMessageSendTaskExecuteDO;
import com.yunxin.core.infra.persistence.mapper.contact.ContactMessageSendTaskExecuteExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Repository
@RequiredArgsConstructor
public class ContactMessageSendTaskExecuteRepositoryImpl implements ContactMessageSendTaskExecuteRepository {

    private final ContactMessageSendTaskExecuteExtendMapper contactMessageSendTaskExecuteExtendMapper;

    private final ContactConverter contactConverter;

    @Override
    public boolean saveContactMessageSendTaskExecute(ContactMessageSendTaskExecute contactMessageSendTaskExecute) {
        ContactMessageSendTaskExecuteDO contactMessageSendTaskExecuteDO = contactConverter.formContactMessageSendTaskExecute(contactMessageSendTaskExecute);
        int count = contactMessageSendTaskExecuteExtendMapper.insertSelective(contactMessageSendTaskExecuteDO);
        contactMessageSendTaskExecute.setId(contactMessageSendTaskExecuteDO.getId());
        return count > 0;
    }

    @Override
    public boolean updateTotalCountById(Long id, int count) {
        int result = contactMessageSendTaskExecuteExtendMapper.updateTotalCountById(id, count);
        return result > 0;
    }

    @Override
    public boolean updateSuccessCountById(Long id, int count) {
        int result = contactMessageSendTaskExecuteExtendMapper.updateSuccessCountById(id, count);
        return result > 0;
    }

    @Override
    public boolean updateReachCountById(Long id, int count) {
        int result = contactMessageSendTaskExecuteExtendMapper.updateReachCountById(id, count);
        return result > 0;
    }

    @Override
    public Long getTaskExecuteIdByTaskDetailAndTime(Long taskDetailId, Date startTime, Date endTime) {
        return contactMessageSendTaskExecuteExtendMapper.selectTaskExecuteIdByTaskDetailAndTime(taskDetailId, startTime, endTime);
    }
}
